# coding=utf-8

from queue import Queue
import threading
from time import sleep
import os

from difflib import SequenceMatcher


class Statistics:
    
    def __init__(self):
        self.album = 0
        self.task = 0
        self.done = 0
        self.lock = threading.Lock()
        
    def __str__(self):
        return "album=%s task=%s done=%s" % (self.album, self.task, self.done)

    def add_album(self):
        self.album += 1
        
    def add_task(self):
        self.task += 1
        
    def add_done(self):
        self.done += 1


statistics = Statistics()

flag_run = True


def p(str1):
    if 0:
        print("%s" % str1)


def wordcount(count_dict, s):

    found = False
    for key in list(count_dict.keys()):
        ret = SequenceMatcher(None, key, s).quick_ratio()
        if ret >= 0.8 :
            found = True
            count_dict[key] += 1
            return
    if not found:
        count_dict[s] = 1


def do_file_2_str(q_lines):
    with open('1.copy.txt', 'rb') as f:
        for s1 in f.readlines():
            q_lines.put(s1)
            statistics.add_task()


def do_str_match(q_lines, d):

    global statistics
    
    while flag_run:
        try:
            line = q_lines.get(timeout=10)
            p("info: do_str_match GET: %s" % line)
        except Exception as e:
            print(e)
            print("info: do_str_match 10s continue")
            continue
        
        wordcount(d, line)
        statistics.add_done()


if __name__ == '__main__':
    d = {}
    
    threads = []
    q_lines = Queue(maxsize=500)

    for i in range(0, 1):
        t = threading.Thread(target=do_file_2_str, args=(q_lines,))
        threads.append(t)

    for i in range(0, 3):
        t = threading.Thread(target=do_str_match, args=(q_lines, d))
        threads.append(t)

    # 开干
    count = range(len(threads))
    for i in count:
        threads[i].start()

    i = 0
    while True:
        sleep(5)
        print("info: q=%s %s" % (q_lines.qsize(), statistics))
        if q_lines.qsize() == 0:
            if i < 6:
                i += 1
            else:
                break
          
    # 收工
    flag_run = False
    for i in count:
        threads[i].join()
    print("info: q=%s %s" % (q_lines.qsize(), statistics))

#     print(count_dict)
    result = sorted(d.items(), key=lambda x:x[1], reverse=True)
    print(result)
    
    print("*** OVER ***")

